﻿@model Himall.Web.Areas.Admin.Models.Market.BonusModel
<script src="~/Scripts/jquery.himallUpload.js"></script>
<link rel="stylesheet" type="text/css" href="~/Content/bootstrap-datetimepicker.min.css">
<script type="text/javascript" src="~/Scripts/bootstrap-datetimepicker.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap-datetimepicker.zh-CN.js"></script>
<style>
    input[type="radio"] {
        margin-top: 9px;
    }

    .w50 {
        width: 50px;
    }
</style>

<div class="container">
    <ul class="nav nav-tabs-custom clearfix">
        <strong>红包管理</strong>
        <li><a href="@Url.Action("Management", "Bonus", new { area = "Admin" })">管理</a></li>
        <li class="active"><a>新增</a></li>
        <li><a href="@Url.Action("Config", "Bonus", new { area = "Admin" })">配置</a></li>
    </ul>


    @using( Html.BeginForm( "Add" , "Bonus" , new { Area = "Admin" } , FormMethod.Post , new { id = "BonusForm"  , @class = "form-horizontal" } ) )
    {

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">红包类型：</label>
            <div class="col-sm-3">
                @Html.RadioButtonFor( p => p.Type , ( int )Himall.Model.BonusInfo.BonusType.Activity , new { @name = "Type" , @id = "rdi1" , @Checked = "checked" } )
                <label for="rdi1">活动红包</label>
                @Html.RadioButtonFor( p => p.Type , ( int )Himall.Model.BonusInfo.BonusType.Attention , new { @name = "Type" , @id = "rdi2" } )
                <label for="rdi2">关注红包</label>
            </div>
            <div id="typemsg" class="col-sm-offset-2 col-sm-10 hide">
                <p class="help-default">会员第一次关注将获得红包，同一时间端内关注送红包活动只能添加一个，请确保红包金额充足</p>
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">红包样式：</label>
            <div class="col-sm-3">
                @Html.RadioButtonFor( p => p.Style , ( int )Himall.Model.BonusInfo.BonusStyle.TempletOne , new { @name = "Style" , @id = "rdi3" , @Checked = "checked" } )
                <label for="rdi3">源生风格</label>
                @*@Html.RadioButtonFor( p => p.Style , ( int )Himall.Model.BonusInfo.BonusStyle.TempletTwo , new { @name = "Style" , @id = "rdi4" } )
                <label for="rdi4">模板二</label>*@
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">商户名称：</label>
            <div class="col-sm-3">
                @Html.TextBoxFor( p => p.MerchantsName , new { @class = "form-control input-sm" , @maxlength = "10" } )
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">分享标题：</label>
            <div class="col-sm-3">
                @Html.TextBoxFor( p => p.Name , new { @class = "form-control input-sm" , @maxlength = "30" } )
            </div>
            <div class="col-sm-6">
                <p class="help-default">活动被分享出去的链接名称，如：清凉一夏，百万现金疯狂抢</p>
            </div>
        </div>
        
        <div class="form-group">
            <label for="" class="col-sm-2 control-label">分享详情：</label>
            <div class="col-sm-3">
                @Html.TextAreaFor( p => p.Remark , new { @class = "form-control input-sm" } )
            </div>
            <div class="col-sm-5">
                <p class="help-default">红包被分享出去后的链接详情</p>
            </div>
        </div>
        
        <div class="form-group">
            <label for="" class="col-sm-2 control-label">分享图标：</label>
            <div class="col-sm-3">
                <div id="upload-img" class="form-group upload-img"></div>
            </div>
            <div class="col-sm-5">
                <p class="help-default">活动被分享出去的链接图标</p>
            </div>
        </div>
        
        <div class="form-group">
            <label for="" class="col-sm-2 control-label">祝福语：</label>
            <div class="col-sm-3">
                @Html.TextBoxFor( p => p.Blessing , new { @class = "form-control input-sm" , @maxlength = "20" } )
            </div>
        </div>
        
        <div class="form-group">
            <label for="" class="col-sm-2 control-label">备注：</label>
            <div class="col-sm-3">
                @Html.TextAreaFor( p => p.Description , new { @class = "form-control input-sm" } )
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">单个面额：</label>
            <div class="col-sm-3">

                @Html.RadioButtonFor( p => p.PriceType , ( int )Himall.Model.BonusInfo.BonusPriceType.Fixed , new { @name = "PriceType" , @id = "rdi5" , @Checked = "checked" } )
                <label for="rdi3">固定金额</label>&nbsp;&nbsp;
                @Html.TextBoxFor( p => p.FixedAmount , new { @class = "w50" , @onkeypress = "return (/[\\d.]/.test(String.fromCharCode(event.keyCode)))" } ) 元 <br />

                @Html.RadioButtonFor( p => p.PriceType , ( int )Himall.Model.BonusInfo.BonusPriceType.Random , new { @name = "PriceType" , @id = "rdi6" } )
                <label for="rdi4">随机金额</label>&nbsp;&nbsp;
                @Html.TextBoxFor( p => p.RandomAmountStart , new { @class = "w50" , @disabled = "true" , @onkeypress = "return (/[\\d.]/.test(String.fromCharCode(event.keyCode)))" } ) 至
                @Html.TextBoxFor( p => p.RandomAmountEnd , new { @class = "w50" , @disabled = "true" , @onkeypress = "return (/[\\d.]/.test(String.fromCharCode(event.keyCode)))" } ) 元
            </div>
            <div class="col-sm-5">
                <p class="help-default">单个红包的面额。最低0.01，最高200</p>
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">红包总面额：</label>
            <div class="col-sm-3">
                @Html.TextBoxFor( p => p.TotalPrice , new { @class = "form-control input-sm" , @onkeypress = "return (/[\\d.]/.test(String.fromCharCode(event.keyCode)))" } )
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">活动开始时间：</label>
            <div class="col-sm-3">
                @Html.TextBoxFor( p => p.StartTime , new { @class = "form-control input-sm start_datetime" } )
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label">活动截止时间：</label>
            <div class="col-sm-3">
                @Html.TextBoxFor( p => p.EndTime , new { @class = "form-control input-sm end_datetime" } )
            </div>
            <div class="col-sm-5">
                <p class="help-default">超过截止时间，未领取完成的红包也无法再领取</p>
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label"></label>
            <div class="col-sm-3">
                <label>关注公众号后可领取</label>
            </div>
        </div>

        <div class="form-group">
            <label for="" class="col-sm-2 control-label"></label>
            <div class="col-sm-5">
                <label>分享后可再次领取（单个用户最多领取两次活动红包）</label>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" id="subCate" class="btn btn-primary">保 存</button>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-9">
                <label style="color:red;">温馨提示：</label><br />
                <label>使用红包进行虚假宣传、恶意诱导用户关注及分享的行为可能会被微信拦截，违规情节特别严重时，微信团队可能会采取永久封禁域名、IP地址或者封停公众号</label>
            </div>
        </div>
    }
</div>

<script type="text/javascript">
    $( function ()
    {
        $( "#subCate" ).click( function ()
        {
            var type = $( 'input[name=Type]:checked' ).val();
            if ( type == 2 )
            {
                $.post( "/Admin/Bonus/CanAdd", {}, function ( result )
                {
                    if ( !result )
                    {
                        $.dialog.tips( '关注红包一个时段只能添加一个！' );
                        return false;
                    }

                    if ( check() )
                    {
                        document.getElementById( "BonusForm" ).submit();
                        $( "#subCate" ).attr( "disabled", true );
                    }
                } )
            }
            else
            {
                if ( check() )
                {
                    document.getElementById( "BonusForm" ).submit();
                    $( "#subCate" ).attr( "disabled", true );
                }
            }

        } )

        $( "input[name=Type]" ).change( function ()
        {
            var self = $( this ).val();
            if ( self == 1 )
            {
                $( "#typemsg" ).addClass( "hide" );
                $( "#Name" ).parents( ".form-group" ).removeClass( "hide" );
                $( "#MerchantsName" ).parents( ".form-group" ).removeClass( "hide" );
                $( "#Remark" ).parents( ".form-group" ).removeClass( "hide" );
                $( "#Blessing" ).parents( ".form-group" ).removeClass( "hide" );
                $( "#Description" ).parents( ".form-group" ).removeClass( "hide" );
                $( "#upload-img" ).parents( ".form-group" ).removeClass( "hide" );
            }
            else if ( self == 2 )
            {
                $( "#typemsg" ).removeClass( "hide" );
                $( "#Name" ).parents( ".form-group" ).addClass( "hide" );
                $( "#MerchantsName" ).parents( ".form-group" ).addClass( "hide" );
                $( "#Remark" ).parents( ".form-group" ).addClass( "hide" );
                $( "#Blessing" ).parents( ".form-group" ).addClass( "hide" );
                $( "#Description" ).parents( ".form-group" ).addClass( "hide" );
                $( "#upload-img" ).parents( ".form-group" ).addClass( "hide" );
            }
        } );
        $( "input[name=PriceType]" ).change( function ()
        {
            var self = $( this ).val();
            if ( self == 1 )
            {
                $( "input[name=FixedAmount]" ).removeAttr( "disabled" );
                $( "input[name=RandomAmountEnd]" ).attr( 'disabled', "true" );
                $( "input[name=RandomAmountStart]" ).attr( 'disabled', "true" );
            }
            else if ( self == 2 )
            {
                $( "input[name=FixedAmount]" ).attr( 'disabled', "true" );
                $( "input[name=RandomAmountEnd]" ).removeAttr( "disabled" );
                $( "input[name=RandomAmountStart]" ).removeAttr( "disabled" );
            }
        } )

        $( "#upload-img" ).himallUpload( {
            title: '',
            imageDescript: '',
            imgFieldName: "ImagePath",
            dataWidth: 10
        } );

        $( ".start_datetime" ).datetimepicker( {
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            autoclose: true,
            weekStart: 1,
            minView: 2
        } );
        $( ".end_datetime" ).datetimepicker( {
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            autoclose: true,
            weekStart: 1,
            minView: 2
        } );

        $( '.start_datetime' ).on( 'changeDate', function ()
        {
            if ( $( ".end_datetime" ).val() )
            {
                if ( $( ".start_datetime" ).val() > $( ".end_datetime" ).val() )
                {
                    $( '.end_datetime' ).val( $( ".start_datetime" ).val() );
                }
            }

            $( '.end_datetime' ).datetimepicker( 'setStartDate', $( ".start_datetime" ).val() );
        } );
    } )

    function check()
    {
        var bonusType = $( 'input[name=Type]:checked' ).val();
        if ( bonusType == 1 )
        {
            if ( $.trim( $( "#Name" ).val() ) == '' )
            {
                $.dialog.tips( '活动名称必填！' );
                return false;
            }
            if ( $.trim( $( "#MerchantsName" ).val() ) == '' )
            {
                $.dialog.tips( '商户名称必填！' );
                return false;
            }
            if ( $.trim( $( "input[name=ImagePath]" ).val() ) == '' )
            {
                $.dialog.tips( '请上传图片！' );
                return false;
            }
            if ( $.trim( $( "#Description" ).val() ) == '' )
            {
                $.dialog.tips( '说明必填！' );
                return false;
            }
            if ( $.trim( $( "#Blessing" ).val() ) == '' )
            {
                $.dialog.tips( '祝福语必填！' );
                return false;
            }
        }

        var type = $( 'input[name=PriceType]:checked' ).val();
        if ( $.trim( $( "#TotalPrice" ).val() ) == '' )
        {
            $.dialog.tips( '总金额必填！' );
            return false;
        }
        var totalPrice = parseFloat( $( "#TotalPrice" ).val() );
        
       
        if ( $.trim( $( "#StartTime" ).val() ) == '' )
        {
            $.dialog.tips( '开始日期必填！' );
            return false;
        }
        if ( $.trim( $( "#EndTime" ).val() ) == '' )
        {
            $.dialog.tips( '结束日期必填！' );
            return false;
        }
        if ( totalPrice > 10000 )
        {
            $.dialog.tips( '总面额不能大于1万！' );
            return false;
        }

        if ( type == 1 )  //固定
        {
            if ( $.trim( $( "#FixedAmount" ).val() ) == '' )
            {
                $.dialog.tips( '固定金额必填！' );
                return false;
            }
            var fixedAmount = parseFloat( $( "#FixedAmount" ).val() );
            if ( fixedAmount > totalPrice )
            {
                $.dialog.tips( '单个面额不能大于总面额！' );
                return false;
            }
            if ( fixedAmount > 200 || fixedAmount < 0.01 )
            {
                $.dialog.tips( '面额错误，取值为0.01 - 200' );
                return false;
            }
            if ( ( totalPrice % fixedAmount ) != 0 )
            {
                $.dialog.tips( '总金额必须是固定金额的倍数！' );
                return false;
            }
        }
        else if ( type == 2 )  //随机
        {
            if ( $.trim( $( "#RandomAmountEnd" ).val() ) == '' )
            {
                $.dialog.tips( '随机金额必填！' );
                return false;
            }
            if ( $.trim( $( "#RandomAmountStart" ).val() ) == '' )
            {
                $.dialog.tips( '随机金额必填！' );
                return false;
            }
            var randomAmountEnd = parseFloat( $( "#RandomAmountEnd" ).val() );
            var randomAmountStart = parseFloat( $( "#RandomAmountStart" ).val() );
            if ( randomAmountEnd > totalPrice )
            {
                $.dialog.tips( '单个面额不能大于总面额！' );
                return false;
            }
            if ( randomAmountStart > randomAmountEnd )
            {
                $.dialog.tips( '随机金额填写错误！' );
                return false;
            }
            if ( randomAmountStart > 200 || randomAmountEnd > 200 || randomAmountStart < 0.01 || randomAmountEnd < 0.01 )
            {
                $.dialog.tips( '面额不能大于200小于0.01！' );
                return false;
            }
        }
        else
        {
            return false;
        }
        return true;
    }
</script>